import json
from utils.e_mail import sendMail
from constant.param_config import getConfigPath
from enum import Enum
from utils.db_util import SqliteTool
from utils.crawl_tool_for_py3_v6 import crawlerTool
from worker.base.base_worker import BaseWorker
# from utils.qianfan_util import QianFan
from constant.param_config import getImmediatelyTime
from utils.log_util import saveLog
from constant.param_config import getConfigPath
from utils.time_util import MMDDHHMM2, YYYYMMDDHHMMSS, stamp_time, current_time
import difflib
import time
import re


class PlatForm(Enum):
    HT = 1  # 汇通网
    ZJ = 2  # 中金网
    HJ = 3  # 黄金网


class BaseArticle(BaseWorker):
    # 汇通网
    baseHtUrl = "https://pinglun.fx678.com"
    htUrl = baseHtUrl + "/category/103?page="

    # 中金网
    baseZjUrl = "http://api.mp.cnfol.com"
    zjUrl = baseZjUrl + "//index/indexcjh/top_articles?callback=jQuery18304712004610123919_1707198178189&category_id=4&page="
    zjEndUrl = "&minid=141242883&_=1707198188931"

    # 黄金网
    hjUrl = "http://shell.cnfol.com/article/gold_article.php?classid=1712&title=&start=0&end=250&apikey=&jsoncallback=jQuery183035263187036094323_1710234751565&_=1710234751568"

    # 现价单匹配
    immediately = ["(现价[^！!。，]*\d{4}[^！!。，]*(多|空))|(\d{4}[^！!。，]*现价(多|空))"]

    startTime = 0
    
    justTitle=False
    login=True

    def __init__(self):
        super().__init__()

        self.request = crawlerTool()
        # self.qianfan = QianFan()

    def initThread(self):
        """
        初始化线程
        """

        tables = ["article_table", "record_table", "tactics_table"]
        dbPath = getConfigPath() + "\data.db"

        self.sqlite = SqliteTool(dbPath=dbPath)
        self.sqlite.create_table_list(tableName=tables)

    def getWebInfo(self, platform, realtime=False):
        """
        1、爬取文章url
        """
        if platform == PlatForm.HT:
            return self.huitong("汇通网", realtime)
        elif platform == PlatForm.ZJ:
            return self.zhongjin("中金网", realtime)
        elif platform == PlatForm.HJ:
            return self.goldSite("黄金网", realtime)

    def goldSite(self, platform, realtime, page=1):
        """
        黄金网
        realtime  实时单
        """
        obj = None
        titleList = []
        try:
            response = self.request.get(self.hjUrl)
            stringData = response.decode('utf-8')
            value = stringData[stringData.index("(") + 1:-1]
            obj = json.loads(value)
        except Exception as e:
            saveLog("黄金网数据获取失败")

        if obj is not None and "content" in obj:
            for item in obj["content"]:
                time = int(item["Time"])
                if current_time() - time < getImmediatelyTime():
                    titleList.append(item["Title"])
        return titleList

    def huitong(self, platform, realtime, page=1):
        """
        汇通网
        realtime  实时单
        """
        titleList = {}
        listItem = None
        try:
            response = self.request.get(self.htUrl + str(page))
            listItem = crawlerTool.getXpath(
                '//li[@class="category-list__item clearfix"]', response)
        except Exception as e:
            saveLog("汇通网数据获取失败")

        if listItem is not None and len(listItem) != 0:
            for index in range(0, len(listItem)):
                try:
                    item = listItem[index]
                    time = crawlerTool.getXpath(
                        '//span[@class="category-list__time"]/text()', item)
                    currentStamp = stamp_time(time[0], MMDDHHMM2)
                    title = crawlerTool.getXpath(
                        '//a[@class="category-list__content"]//h3/text()',
                        item)[0]
                    url = self.baseHtUrl + crawlerTool.getXpath(
                        '//a/@href', item)[0]

                    if realtime is False:
                        if currentStamp <= self.startTime:
                            break

                        maxId = self.sqlite.query_one(
                            "SELECT MAX(id) FROM article_table")
                        id = 0
                        if len(maxId) != 0 and maxId[0] is not None:
                            id = maxId[0] + 1

                        self.sqlite.operate_one(
                            'insert into article_table values(?,?,?,?,?,?,?,?)',
                            (id, title, url, "", "", "", platform,
                             currentStamp))
                    elif current_time() - currentStamp < getImmediatelyTime():
                        titleList[currentStamp] = title
                except Exception as e:
                    saveLog("序号" + str(item[0]) + "url解析错误")

                if index == len(listItem) - 1:
                    if realtime is False:
                        page = page + 1
                        self.huitong(platform, realtime, page)
                    else:
                        latest = max(titleList.items(), key=lambda x: x[0]
                                     )[1] if len(titleList) != 0 else ""
                        return [latest] if latest != "" else []

        if page <= 2 and realtime is False and self.justTitle is False:
            self.articleContent(platform,
                                mainRe='//div[@class="article-cont"]')

    def zhongjin(self, platform, realtime, page=1):
        """
        中金
        realtime  实时单
        """
        obj = None
        titleList = {}
        try:
            response = self.request.get(self.zjUrl + str(page) + self.zjEndUrl)
            stringData = response.decode('utf-8')
            value = stringData[stringData.index("(") + 1:-2]
            obj = json.loads(value)
        except Exception as e:
            saveLog("中金网数据获取失败")

        if obj is not None and "data" in obj:
            listItem = obj["data"]
            for index in range(0, len(listItem)):
                item = listItem[index]
                currentStamp = stamp_time(item["create_time"], YYYYMMDDHHMMSS)
                title = item["title"]

                if realtime is False:
                    if currentStamp <= self.startTime:
                        break

                    if "url" not in item:
                        continue

                    url = item["url"]
                    maxId = self.sqlite.query_one(
                        "SELECT MAX(id) FROM article_table")
                    id = 0
                    if len(maxId) != 0 and maxId[0] is not None:
                        id = maxId[0] + 1

                    self.sqlite.operate_one(
                        'insert into article_table values(?,?,?,?,?,?,?,?)',
                        (id, title, url, "", "", "", platform, currentStamp))
                elif current_time() - currentStamp < getImmediatelyTime():
                    titleList[currentStamp] = title

                if index == len(listItem) - 1:
                    if realtime is False:
                        page = page + 1
                        self.zhongjin(platform, realtime, page)
                    else:
                        latest = max(titleList.items(), key=lambda x: x[0]
                                     )[1] if len(titleList) != 0 else ""
                        return [latest] if len(latest) != 0 else []

        if page <= 2 and realtime is False and self.justTitle is False:
            self.articleContent(platform, mainRe='//div[@class="mainInfo"]')

    def articleContent(self, platform, mainRe):
        """
        文章正文
        """
        newArticle = self.sqlite.query_many(
            "SELECT id,url,time FROM article_table WHERE article = '' and platform = '"
            + platform + "' and time > " + str(self.startTime))

        if newArticle is not None and len(newArticle) != 0:
            for item in newArticle:
                try:
                    content = self.request.get(item[1])
                    article = self.request.getXpath(mainRe, content)

                    results = self.request.getXpath('//p/strong/text()',
                                                    article[0])
                    if len(results) == 0:
                        results = self.request.getXpath(
                            '//p/text()', article[0])
                    value = re.sub(r"\s+", "", "".join(results))

                    sameContent = []
                    contentList = self.sqlite.query_many(
                        "SELECT article FROM article_table WHERE article != ''"
                    )
                    if contentList is not None and len(contentList) != 0:
                        sameContent = [
                            item
                            for item in contentList if difflib.SequenceMatcher(
                                None, item[0], value).quick_ratio() > 0.9
                        ]
                    if len(sameContent) == 0:
                        self.sqlite.operate_one(
                            "update article_table set article=? where id=?",
                            (value, item[0]))
                except Exception as e:
                    saveLog("序号" + str(item[0]) + "文章内容解析错误")
                time.sleep(3)
